package com.example.service_cms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.commonutils.ResultData;
import com.example.service_cms.entity.CrmBanner;
import com.example.service_cms.service.CrmBannerService;
import com.example.servicebase.client.OssClient;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-05-30
 */
@RestController
@RequestMapping("/educms/banneradmin")
@MapperScan("com.example.service_cms.mapper")
public class BannerAdminController {
    @Autowired
    private CrmBannerService bannerService;
    @Autowired
    private OssClient ossClient;

    @ApiOperation(value = "获取Banner分页列表")
    @PostMapping("{page}/{limit}")
    public ResultData index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) CrmBanner banner) {

        Page<CrmBanner> pageParam = new Page<>(page, limit);
        QueryWrapper<CrmBanner> wrapper=new QueryWrapper<>();
        String title = banner.getTitle();
        if (!StringUtil.isNullOrEmpty(title)){
            wrapper.like("title",title);
        }
        wrapper.orderByDesc("sort");
        bannerService.page(pageParam, wrapper);
        long total = pageParam.getTotal();
        return ResultData.ok().data("items", pageParam.getRecords()).data("total", total);
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public ResultData get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return ResultData.ok().data("item", banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping("save")
    public ResultData save(@RequestBody CrmBanner banner) {
        bannerService.save(banner);
        return ResultData.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public ResultData updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        return ResultData.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public ResultData remove(@PathVariable String id) {
/*
        String str="https://online-learn000.oss-cn-chengdu.aliyuncs.com/2022/05/30/5afeede8b26f40d496b0d2d0f01e0d12two.jpg";
        //先查询banner
        CrmBanner banner = bannerService.getById(id);
        String imageUrl = banner.getImageUrl();
        if (!str.equals(imageUrl)){
            UrlObject url=new UrlObject();
            url.setUrl(imageUrl);
            //发起远程调用去删除图片
            ossClient.deletePhoto(url);
        }
*/

        bannerService.removeById(id);
        return ResultData.ok();
    }
}

